/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Services;

import database.daos.ProviderDAO;
import database.daos.StoreDAO;
import database.daos.StoreEmployeeDAO;
import database.daos.UserDAO;
import database.daos.DAOException;
import Services.exception.AlreadyExistBeanException;
import Services.exception.EntityNotFoundException;
import Services.exception.ServiceException;
import Services.exception.UserTypeIdException;
import Services.exception.UnableToConnectException;
import Validations.ProviderValidation;
import Validations.StoreEmployeeValidation;
import Validations.StoreValidation;
import Validations.UserValidation;
import beans.ProviderBean;
import beans.StoreBean;
import beans.StoreEmployeeBean;
import beans.UserBean;


/**
 *
 * @author Anthar
 */
public class AdministratorService {
    private ProviderValidation provValidation;
    private ProviderDAO provDAO;
    private ProviderServices providerService;
    private UserValidation userValidation;
    private UserDAO userDAO;
    private UserServices userService;
   // private StoreServices storeService;
   private StoreValidation storeValidation;
   private StoreDAO storeDAO;
   private StoreEmployeeValidation storeEmployeeValidation;
   private StoreEmployeeDAO storeEmployeeDAO;

    public AdministratorService(){
          provValidation = new ProviderValidation();
          provDAO = new ProviderDAO();
          providerService = new ProviderServices();
          userValidation = new UserValidation();
          userDAO = new UserDAO();
          userService = new UserServices();
          storeValidation = new StoreValidation();
          storeDAO = new StoreDAO();
          storeEmployeeValidation = new StoreEmployeeValidation();
          storeEmployeeDAO = new StoreEmployeeDAO();

    }


    public void addProvider(ProviderBean provInfo) throws  ServiceException {
        if (!getProvValidation().existProvider(provInfo.getProviderRFC())) {
            try {
                getProvDAO().insert(provInfo);
            } catch (DAOException ex) {
               throw new UnableToConnectException(ex.getMessage());
            }
        }else{
            // se podria reciclar la excepcion del usuario o una nueva?
           throw new AlreadyExistBeanException("Usuario");
        }
    }


     public void createUserAccount(UserBean userInfo) throws ServiceException{
        try {
            if (!getUserValidation().existUserAccount(userInfo.getUserAccountName())) {
                if (getUserValidation().validTypeUser(userInfo.getUserTypeId())) {
                      getUserDAO().insert(userInfo);
                  }
                else{
                    throw new UserTypeIdException("El id se usuario no es valido");
                }
            }
            else {
                throw new AlreadyExistBeanException("Usuario ya existente");
            }
        } catch (DAOException ex) {
            throw new UnableToConnectException(ex.getMessage());
        }      
     }

     public void deleteUserAccount(String userAccountName) throws ServiceException{
         if(getUserValidation().existUserAccount(userAccountName)){
              try {
                getUserDAO().delete(userAccountName);
            } catch (DAOException ex) {
                throw new UnableToConnectException(ex.getMessage());
            }
         }else{
            throw new EntityNotFoundException("User");
        }
     }

      public void addNewStore(StoreBean store) throws ServiceException{
       if(!getStoreValidation().existStore(store.getStoreRFC())){
            try {
                getStoreDAO().insert(store);
            } catch (DAOException ex) {
                throw new UnableToConnectException(ex.getMessage());
            }
       }else{
           throw new AlreadyExistBeanException("Store");
       }
     }


     public void deleteStore(String RFC) throws ServiceException{
        if(getStoreValidation().existStore(RFC)){
            try {
                getStoreDAO().delete(RFC);
            } catch (DAOException ex) {
                throw new UnableToConnectException(ex.getMessage());
            }
        }else{
            throw new EntityNotFoundException("Store");
        }
     }

     
     public void addNewStoreEmployee(StoreEmployeeBean storeEmployee) throws ServiceException{
         if(!getStoreEmployeeValidation().existStoreEmployee(storeEmployee.getStoreEmployeeId())){
            try {
                getStoreEmployeeDAO().insert(storeEmployee);
            } catch (DAOException ex) {
                throw new UnableToConnectException(ex.getMessage());
            }             
         }else{
             throw new AlreadyExistBeanException("Saler");
         }
     }

     public void deleteStoreEmployee(int idStoreEmployee) throws ServiceException{
         if(getStoreEmployeeValidation().existStoreEmployee(idStoreEmployee)){
           try {
                getStoreEmployeeDAO().deleteStoreEmployee(idStoreEmployee);
            } catch (DAOException ex) {
               throw new UnableToConnectException(ex.getMessage());  
            }
         }else{
            throw new EntityNotFoundException("StoreEmployee");
        }

     }
    

    /***************************
     *  SETTERS Y GETTERS
     ***************************/

    /**
     * @return the provValidation
     */
    public ProviderValidation getProvValidation() {
        return provValidation;
    }

    /**
     * @param provValidation the provValidation to set
     */
    public void setProvValidation(ProviderValidation provValidation) {
        this.provValidation = provValidation;
    }

    /**
     * @return the provDAO
     */
    public ProviderDAO getProvDAO() {
        return provDAO;
    }

    /**
     * @param provDAO the provDAO to set
     */
    public void setProvDAO(ProviderDAO provDAO) {
        this.provDAO = provDAO;
    }

    /**
     * @return the userValidation
     */
    public UserValidation getUserValidation() {
        return userValidation;
    }

    /**
     * @param userValidation the userValidation to set
     */
    public void setUserValidation(UserValidation userValidation) {
        this.userValidation = userValidation;
    }

    /**
     * @return the userDAO
     */
    public UserDAO getUserDAO() {
        return userDAO;
    }

    /**
     * @param userDAO the userDAO to set
     */
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    /**
     * @return the providerService
     */
    public ProviderServices getProviderService() {
        return providerService;
    }

    /**
     * @param providerService the providerService to set
     */
    public void setProviderService(ProviderServices providerService) {
        this.providerService = providerService;
    }

    /**
     * @return the userService
     */
    public UserServices getUserService() {
        return userService;
    }

    /**
     * @param userService the userService to set
     */
    public void setUserService(UserServices userService) {
        this.userService = userService;
    }  

    /**
     * @return the storeValidation
     */
    public StoreValidation getStoreValidation() {
        return storeValidation;
    }

    /**
     * @param storeValidation the storeValidation to set
     */
    public void setStoreValidation(StoreValidation storeValidation) {
        this.storeValidation = storeValidation;
    }

    /**
     * @return the storeDAO
     */
    public StoreDAO getStoreDAO() {
        return storeDAO;
    }

    /**
     * @param storeDAO the storeDAO to set
     */
    public void setStoreDAO(StoreDAO storeDAO) {
        this.storeDAO = storeDAO;
    }

    /**
     * @return the storeEmployeeValidation
     */
    public StoreEmployeeValidation getStoreEmployeeValidation() {
        return storeEmployeeValidation;
    }

    /**
     * @param storeEmployeeValidation the storeEmployeeValidation to set
     */
    public void setStoreEmployeeValidation(StoreEmployeeValidation storeEmployeeValidation) {
        this.storeEmployeeValidation = storeEmployeeValidation;
    }

    /**
     * @return the storeEmployeeDAO
     */
    public StoreEmployeeDAO getStoreEmployeeDAO() {
        return storeEmployeeDAO;
    }

    /**
     * @param storeEmployeeDAO the storeEmployeeDAO to set
     */
    public void setStoreEmployeeDAO(StoreEmployeeDAO storeEmployeeDAO) {
        this.storeEmployeeDAO = storeEmployeeDAO;
    }

}
